package com.huike.contract.service.impl;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.contract.service.ITbTransferService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author YEmeeyo
 * @Date 2023-07-13 9:16
 */
@Service
public class TbTransferServiceImpl implements ITbTransferService {

    @Resource
    private TbContractMapper contractMapper;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private TbAssignRecordMapper assignRecordMapper;

    @Resource
    private TbClueMapper clueMapper;

    @Resource
    private TbBusinessMapper businessMapper;

    /**
     * 获取转派列表
     *
     * @param user
     * @return
     */
    @Override
    public List<TransferVo> list(SysUser user) {
        List<TransferVo> transferVoList = new ArrayList<>();
        List<SysUser> userList = userMapper.selectUserList(user);

        for (SysUser sysUser : userList) {
            Integer clueNum = assignRecordMapper.getClueCountByUserId(sysUser.getUserId());
            Integer businessNum = assignRecordMapper.getBusinessCountByUserId(sysUser.getUserId());
            if (clueNum + businessNum > 0) {
                TransferVo transferVo = new TransferVo();
                transferVo.setUserId(sysUser.getUserId());
                transferVo.setUserName(sysUser.getUserName());
                transferVo.setPhonenumber(sysUser.getPhonenumber());
                transferVo.setClueNum(clueNum);
                transferVo.setBusinessNum(businessNum);
                transferVo.setCreateTime(sysUser.getCreateTime());
                transferVoList.add(transferVo);
            }
        }
        return transferVoList;
    }

    /**
     * 转派处理
     *
     * @param type
     * @param userId
     * @param transferUserId
     * @return
     */
    @Override
    public void assignment(String type, Long userId, Long transferUserId) {
        //获取转移人的信息
        SysUser transferUser = userMapper.selectUserById(transferUserId);
        //先判断转派的类型是线索还是商机
        //这里是判断是线索
        if (type.equals(TbAssignRecord.RecordType.CLUES.getValue())) {
            //创建一个新的TbAssignRecord,用于封装查询条件
            TbAssignRecord tbAssignRecord = new TbAssignRecord();
            tbAssignRecord.setUserId(userId);
            tbAssignRecord.setType(TbAssignRecord.RecordType.CLUES.getValue());
            //获取到所有符合条件的线索
            List<TbAssignRecord> clueAssignRecord = assignRecordMapper.selectAssignRecordList(tbAssignRecord);

            //遍历循环把每个线索都更新一下信息
            for (TbAssignRecord assignRecord : clueAssignRecord) {
                //封装新记录信息
                assignRecord.setUserId(transferUserId);
                assignRecord.setUserName(transferUser.getUserName());
                assignRecord.setDeptId(transferUser.getDeptId());
                //更新线索信息
                assignRecordMapper.updateAssignRecord(assignRecord);
                //把转派的线索的状态更新一下
                clueMapper.updateTransfer(assignRecord.getAssignId(), TbClue.StatusType.UNFOLLOWED.getValue());
            }
        }

        //这里是判断是商机
        if (type.equals(TbAssignRecord.RecordType.CLUES.getValue())) {
            //创建一个新的TbAssignRecord,用于封装查询条件
            TbAssignRecord tbAssignRecord = new TbAssignRecord();
            tbAssignRecord.setUserId(userId);
            tbAssignRecord.setType(TbAssignRecord.RecordType.BUSNIESS.getValue());
            //获取到所有符合条件的商机
            List<TbAssignRecord> businessAssignRecord = assignRecordMapper.selectAssignRecordList(tbAssignRecord);

            //遍历循环把每个商机都更新一下信息
            for (TbAssignRecord assignRecord : businessAssignRecord) {
                //封装新记录信息
                assignRecord.setUserId(transferUserId);
                assignRecord.setUserName(transferUser.getUserName());
                assignRecord.setDeptId(transferUser.getDeptId());
                //更新线索信息
                assignRecordMapper.updateAssignRecord(assignRecord);
                //把转派的线索的状态更新一下
                businessMapper.updateTransfer(assignRecord.getAssignId(), TbBusiness.StatusType.UNFOLLOWED.getValue());
            }
        }
    }
}
